<?php
require 'lib/XS.php';

class xunsearch {
	public $proj_name = '';
	
	//建立搜索对象
	public function xsearch(){
		if($this->proj_name == ''){
			return '项目名不能为空';
		}
		
		$xs = new XS($this->proj_name);
		$search = $xs->search;
		
		return $search;
	}
	
	//获取缩影库数据总数
	public function getTotal(){
		$search = $this->xsearch();
		$total = $search->getDbTotal();
		
		return $total;
	}
	
	//获取总行数
	public function xxount($searchproj){
		$search = $this->xsearch();
		$count = $search->count($searchproj); 
		
		return $count;
	}
	
	//获取所有数据
	public function Allsearch($startrow,$searchrow,$searchproj,$highlight=array(),$order=''){
		try{
			$search = $this->xsearch();
			
			$search->setLimit($searchrow,$startrow);
			$search->setCharset('utf-8');
			
			if($order){
				$search->setSort($order); 
			}
			
			$data = $search->search($searchproj);
			//$data = $search->setFuzzy()->search($searchproj);
			
			$arr = array();
					
			//结果高亮梳理
			foreach($data as $key => $doc){
				if(!empty($highlight)){
					foreach($doc as $k => $v){
						if(in_array($k,$highlight)){
							$arr[$key][$k] = $search->highlight($doc[$k]);
						}else{
							$arr[$key][$k] = $v;
						}
					}
				}else{
					$arr = $data;
				}
				
			   /* $arr[$key]['title'] = $search->highlight($doc['title']);
			   $arr[$key]['content'] = $search->highlight($doc['content']); 
			   
			   foreach($doc as $k => $value){
				   if($k != 'title' and $k != 'content')
					$arr[$key][$k] = $value;
			   } */
			}
			//var_dump($arr);exit;
			return $arr;
		}
		catch (XSException $e){
			echo $e;               // 直接输出异常描述
			if (defined('DEBUG'))  // 如果是 DEBUG 模式，则输出堆栈情况
				echo "\n" . $e->getTraceAsString() . "\n";
		}
	}
	
	//建立索引对象
	public function xindex(){
		if($this->proj_name == ''){
			echo '项目名不能为空';
		}
		
		try{
			$xs = new XS($this->proj_name); 
			$index = $xs->index;
			
			return $index;
		}
		catch (XSException $e){
			echo $e;               // 直接输出异常描述
			if (defined('DEBUG'))  // 如果是 DEBUG 模式，则输出堆栈情况
				echo "\n" . $e->getTraceAsString() . "\n";
		}
	}
	
	//添加索引数据
	public function xaddindex($data){
		$index = $this->xindex();
		
		$doc = new XSDocument;
		$doc->setFields($data);
	 
		// 添加到索引数据库中
		$res = $index->add($doc);
		if($res){
			return true;
		}else{
			return false;
		}
		
	}
	
	//修改索引数据
	public function updateindex($data){
		$index = $this->xindex();
		
		$doc = new XSDocument;
		$doc->setFields($data);
		
		//修改索引
		$index->update($doc);
		
		return true;
	}
	
	//删除索引数据 如果$field为空按主键删除 否则按字段加索引删除
	public function delindex($data,$field=''){
		$index = $this->xindex();
				
		//删除索引数据
		if($field == '')
			$index->del($data);
		else
			$index->del($data,$field);
		
		return true;
	}
	
	//清空项目所有索引数据
	public function delAll(){
		$index = $this->xindex();
		$index->clean();
		
		return true;
	}
	
	//获取项目所有字段
	public function dield_data(){
		$xs = new XS($this->proj_name); 
		
		$data = $xs->allFields;
		
		$arr = array();
		foreach($data as $k => $v){
			$arr[$k] = $k;
		}
		
		return $arr;
	}
	
	
	//文件转text
	public function docToText($fileurl){
		$output = '';
		
		exec('D:\php\htdocs\edu270_1.1\lib\xunsearch\doctotext\doctotext '.$fileurl , $output);
		
		if($output){
			$output = implode(',',array_filter($output));//清除空元素并转数组为字符串
		}
		
		return $output;
	}
	
	
	
} 

